package com.zking.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zking.dao.QuestionsMapper;
import com.zking.dao.UsersMapper;
import com.zking.model.Questions;
import com.zking.model.QuestionsExample;
import com.zking.model.Users;
import com.zking.model.UsersExample;
import com.zking.service.UserService;
import com.zking.util.AppException;
import com.zking.util.MD5;
@Service("userService")
@Transactional(rollbackFor=Exception.class)
public class UserServiceImpl implements UserService{
	@Autowired
	private UsersMapper usermapper;
	@Autowired
	private QuestionsMapper questionmapper;
	//注册
		public int Register(Users user) throws AppException {
			try {
				if(usermapper.selectUserByEmail(user.getuEmail())!=null){
					return 0;
				}
				else{
					return usermapper.insertSelective(user);
				}
			}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				throw new AppException("数据异常",e);
			}
		}

	
		//登录验证
		public Users validateLogin(String email,String pwd) throws AppException {
			MD5 md5 = new MD5();
			Users user=null; 
			UsersExample example = new UsersExample();
			example.createCriteria().andUPwdEqualTo(md5.DoMD5(pwd)).andUEmailEqualTo(email);
			try {
				if(usermapper.selectByExample(example).size()>0){
					user = usermapper.selectByExample(example).get(0);
				}
			} catch (SQLException e) {
				throw new AppException("数据异常",e);
			}
			return user;
		}
		
		//判断用户是否登录
		public Users pdLogin(String email) throws AppException {
			Users user=null; 
			UsersExample example = new UsersExample();
			example.createCriteria().andUEmailEqualTo(email);
			try {
				if(usermapper.selectByExample(example).size()>0){
					user = usermapper.selectByExample(example).get(0);
				}
			} catch (SQLException e) {
				throw new AppException("数据异常",e);
			}
			return user;
		}
		//注册邮箱验证
		public int checkEmail(String email) throws AppException {
			// TODO Auto-generated method stub
			int num=0;
			
			UsersExample example=new UsersExample();
			example.createCriteria().andUEmailEqualTo(email);
			
			try {
				if(usermapper.selectByExample(example).size()>0){
					num=1;
				}
			} catch (SQLException e) {
				throw new AppException("数据异常",e);
			}
			
			return num;
		}
		
		//通过用户ID查找用户的详细信息
		public Users selectUsersByuid(int uId) throws AppException{
			try {
				return usermapper.selectByPrimaryKey(uId);
			} catch (SQLException e) {
				throw new AppException("数据异常",e);
			}
		}
		//修改用户的基本信息
		public int updateUserInfo(int uId,String uName,String uPhone)throws AppException{
			try {
				return usermapper.updateUserInfo(uId, uName, uPhone);
			} catch (SQLException e) {
				throw new AppException("数据异常",e);
			}
		}
		//修改用户的密码
		public int updatePwd(int uId,String uPwd)throws AppException{
			 try {
				return usermapper.updatePwd(uId, uPwd);
			} catch (SQLException e) {
				throw new AppException("数据异常",e);
			}
		}
		
		
}
